<template>
	<view class="content">
		<view class="form">
			<view class="title">学创签电子合同【{{ loginName }}】</view>
			<view style="margin-top: 22rpx;">
				<u-form ref="loginFormRef" :model="loginForm.data" :rules="loginForm.rule">
					<u-form-item label="账号" prop="cellphone" borderBottom labelWidth="70" customStyle="marginTop:20rpx">
						<u-input v-model="loginForm.data.cellphone" customStyle="height:56rpx" border="none"
							maxlength='11' type='number' placeholder="输入手机号"></u-input>
					</u-form-item>
					<u-form-item label="验证码" labelWidth="70" prop="code" borderBottom v-if="show"
						customStyle="marginTop:20rpx">
						<u-input v-model="loginForm.data.code" border="none" customStyle="height:56rpx" maxlength='6'
							type='number' placeholder="输入验证码"></u-input>
						<template v-slot:right>
							<view @click="sendSms">
								<sms ref="smsC"></sms>
							</view>
						</template>
					</u-form-item>
					<u-form-item label="密码" labelWidth="70" prop="password" borderBottom v-if="!show"
						customStyle="marginTop:20rpx">
						<u-input v-model="loginForm.data.password" placeholder="输入密码" border="none" type="password"
							customStyle="height:56rpx;"></u-input>
					</u-form-item>
				</u-form>
				<view class="bottom-button">
					<u-button type="primary" text="登录" class="custom-style" @click="loginForm.submit"></u-button>
					<view style="margin-top: 20rpx;">
						<u-button type="success" text="注册" class="custom-style"
							@click="goNav('/pages/alogin/register')"></u-button>
					</view>
				</view>
			</view>
			<view class="_bottom" @click="changeType">
				<text>切换登录方式</text>
				<image src="../../static/images/phone.png" v-if="show"></image>
				<image src="../../static/images/login1.png" v-else></image>
				<text>{{ show === true ? "手机号登录" : '验证码登录' }}</text>
			</view>
		</view>
		<u-toast :type="type" ref="uToast"></u-toast>
	</view>
</template>
<script setup>
	import {
		sms
	} from '@/components/sms.vue'
	import {
		cellphoneLogin,
		enterpriseLogin
	} from "../../api/module/login.js"
	import {
		reactive,
		ref
	} from 'vue'
	import {
		arrayGet,
		goNav,
		goSwitchTab,
		mobileRegular,
		msg,
		pageUrl
	} from "../../utils/common.js"
	import {
		onShow,
		onLoad
	} from "@dcloudio/uni-app";
	import {
		useStore
	} from "vuex";
	import {
		customStyle,
		customStyle1
	} from "../../utils/style";
	import {
		contractList,
		membercontractList
	} from "../../api/module/contract";
	import {
		systemPermissions
	} from "../../api/module/mine";
	const smsC = ref();
	const show = ref(false);
	const store = useStore();
	const type = ref(1);
	const userType = ref(store.state.userType);
	const loginName = ref('个人端');
	onLoad(() => {
		switch (userType.value) {
			case 1:
				loginForm.data.cellphone = '';
				break;
			case 1:
				loginForm.data.cellphone = '';
				break;
		}
	});
	/**
	 * 登录
	 * @type {Ref<UnwrapRef<null>>}
	 */
	const loginFormRef = ref(null)
	const loginForm = reactive({
		data: ref({
			cellphone: '',
			code: '',
			password: '',
		}),
		formRule: function() {
			return ref({
				cellphone: [{
					required: true,
					message: '请输入手机号',
					trigger: 'blur'
				}, {
					pattern: mobileRegular,
					message: '手机号格式不正确',
					trigger: 'blur'
				}],
				code: [{
					required: show.value,
					message: '请输入验证码',
					trigger: 'blur'
				}],
				password: [{
					required: !show.value,
					message: '请输入密码',
					trigger: 'blur'
				}],
			})
		},
		rule: {},
		submit: function() {
			loginFormRef.value.setRules(loginForm.formRule());
			loginFormRef.value.validate().then(validate => {
				if (validate) {
					login();
				}
			});
		}
	})
	const sendSms = () => {
		smsC.value.send({
			phone: loginForm.data.cellphone,
			type: "login_code",
		})
	}
	onShow(() => {
		loginForm.rule = loginForm.formRule();
		type.value = store.state.userType
		loginName.value = store.state.userType == 1 ? '个人端' : '企业端';
	});
	const register = () => {
		console.log('注册');
	}
	// 切换登录方式
	const changeType = () => {
		show.value = !show.value
		if (show.value) {
			// loginForm.data.cellphone = '13426386684';
			// loginForm.data.cellphone = '15001002938';
			// loginForm.data.code = '123456';
			// loginForm.data.cellphone = '';
			loginForm.data.code = ''
		} else {
			loginForm.data.password = ''
		}
		loginForm.rule = loginForm.formRule();
	}
	// 登录
	const login = () => {
		let params
		if (show.value) {
			params = {
				cellphone: loginForm.data.cellphone,
				code: loginForm.data.code,
			}
		} else {
			params = loginForm.data
		}
		if (store.state.userType == 1) { //登录用户
			cellphoneLogin(params).then(res => {
				uni.setStorageSync("token", res.token)
				uni.setStorageSync("userInfo", res.member)
				if (!res.token) {
					msg('账号密码错误');
					return;
				}
				if (res.member.set_password_status == 2) {
					goNav('/pages/alogin/setUserInfo');
					return;
				}
				if (arrayGet(res, 'member.certification_status') != 1) {
					// 调用uni.showModal()函数显示确认取消提示框
					uni.showModal({
						title: '提示', // 标题
						content: '您尚未实名认证，请实名认证后再操作', // 内容
						cancelText: "忽略", // 取消按钮的文字
						confirmText: "去认证", // 确认按钮文字
						success(res) {
							if (res.confirm) {
								goNav(arrayGet(pageUrl, 'realnameUrl.1'));
								// 这里写取消操作的相关代码
							} else if (res.cancel) {
								goSwitchTab('/pages/home/index');
							}
						}
					});
					return;
				}
				membercontractList({
					limit: 10,
					sign_status: 'pending',
				}).then(response => {
					if (response.count > 0) {
						uni.showModal({
							title: '提示', // 标题
							content: '您尚有未签署的合同，请及时处理', // 内容
							cancelText: "忽略", // 取消按钮的文字
							confirmText: "去签署", // 确认按钮文字
							success(res) {
								if (res.confirm) {
									goSwitchTab('/pages/contract/index');
									// 这里写取消操作的相关代码
								} else if (res.cancel) {
									goSwitchTab('/pages/home/index');
								}
							}
						});
						return true;
					}
					goSwitchTab('/pages/home/index');
				});
			}).catch(err => {
				uni.$u.toast(err)
			})
		} else { //登录企业
			enterpriseLogin(params).then(res => {
				uni.setStorageSync("token", res.token)
				uni.setStorageSync("userInfo", res.user)
				if (res.user.set_password_status == 2) {
					goNav('/pages/alogin/setUserInfo')
				}
				systemPermissions().then(response => {
					uni.setStorageSync("permissions", response)
				})
				contractList({
					limit: 10,
					sign_status: 'pending',
				}).then(response => {
					if (response.count > 0) {
						uni.showModal({
							title: '提示', // 标题
							content: '您尚有未签署的合同，请及时处理', // 内容
							cancelText: "忽略", // 取消按钮的文字
							confirmText: "去签署", // 确认按钮文字
							success(res) {
								if (res.confirm) {
									goSwitchTab('/pages/contract/index');
									// 这里写取消操作的相关代码
								} else if (res.cancel) {
									goSwitchTab('/pages/home/index');
								}
							}
						});
						return true;
					}
					goSwitchTab('/pages/home/index');
				});
			}).catch(err => {
				uni.$u.toast(err)
			})
		}
	}
</script>
<style lang="scss" scoped src="/static/css/login.scss"></style>